package com.zlp;

import java.util.*;

public class TopVotedCandidate {
    int[] ret;
    Map<Integer,Integer> cnt = new HashMap<>();
    TreeMap<Integer,Integer> map = new TreeMap<>();
    public TopVotedCandidate(int[] persons, int[] times) {
        for (int i=0;i<times.length;i++){
            map.put(times[i],i);
        }
        int max = 0,top = 0;
        ret = new int[persons.length];
        for (int i=0;i<persons.length;i++){
            cnt.put(persons[i],cnt.getOrDefault(persons[i],0)+1);
            if(cnt.get(persons[i])>=max){
                max = cnt.get(persons[i]);
                top = persons[i];
            }
            ret[i] = top;
        }
    }

    public int q(int t) {
        int index = map.get(map.floorKey(t));
        return ret[index];
    }

    public static void main(String[] args) {
        new TopVotedCandidate(new int[]{0,1,0,1,1},new int[]{});
    }

    public String toLowerCase(String s) {
        StringBuilder sb = new StringBuilder();
        for (char ch : s.toCharArray()) {
            if (ch > 'A' && ch < 'Z') sb.append((char)(ch + 32));
            else sb.append(ch);
        }
        return sb.toString();
    }

    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int rlen = grid.length,clen = grid[0].length;
        int[] row = new int[rlen],column = new int[clen];
        for (int i=0;i<rlen;i++){
            for (int j=0;j<clen;j++){
                column[i] = Math.max(column[i],grid[i][j]);
                row[j] = Math.max(row[j],grid[i][j]);
            }
        }
        int ret = 0;
        for (int i=0;i<rlen;i++){
            for (int j=0;j<clen;j++){
                ret += Math.min(row[i],column[j]) - grid[i][j];
            }
        }
        return ret;
    }

    public int scheduleCourse(int[][] courses) {
        Arrays.sort(courses,(o1, o2) -> o1[1] -o2[1]);
        int cur = 0;
        PriorityQueue<int[]> queue = new PriorityQueue((o1, o2) -> o2[0]-o1[0]);
        for (int[] course:courses){
            if(cur+course[0]<=course[1]){
                queue.offer(course);
                cur += course[0];
            }else if(!queue.isEmpty()&&queue.peek()[0]>course[0]){
                cur = cur + course[0] - queue.poll()[0];
                queue.offer(course);
            }
        }
        return queue.size();
    }
    public int repeatedStringMatch(String a, String b) {


        return -1;
    }


}
